﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel.Composition;
using System.ComponentModel.Composition.Hosting;
using System.Linq;
using System.Net;
using System.IO;
using System.Reflection;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;

namespace G.APITest.ChildWindows
{
    public partial class CreateProjectChildWindow : ChildWindow
    {
        public CreateProjectChildWindow(APITest.Config.APIProjectConfig config = null)
        {
            InitializeComponent();
            listbox_setupd.ItemsSource = Utils.ProjectTemplateTableUtils.LoadTable();
            if (config != null)
            {
                grid_config.Visibility = System.Windows.Visibility.Collapsed;
            }
        }

        private void OKButton_Click(object sender, RoutedEventArgs e)
        {
            G.APITest.Data.ProjectTemplate t = listbox_setupd.SelectedItem as G.APITest.Data.ProjectTemplate;
            SelectedProjectTemplate = t;
            this.DialogResult = true;
        }

        private void CancelButton_Click(object sender, RoutedEventArgs e)
        {
            this.DialogResult = false;
        }

        public Data.ProjectTemplate SelectedProjectTemplate { get; private set; }

        [Import(typeof(G.APITest.Part.IAPIRequestPart))]
        public G.APITest.Part.IAPIRequestPart APIRequestPart { get; set; }

        private void LoadDLLFromLocal(object sender, RoutedEventArgs e)
        {
            APIRequestPart = null;
            Utils.MEFUtils.AddPartToIso(this);
            //OpenFileDialog open = new OpenFileDialog();
            //open.Multiselect = true;
            //if (open.ShowDialog() == true)
            //{
            //    G.SL.Net.HttpFileUpload hfp = new SL.Net.HttpFileUpload("http://localhost:4658/APITest/APITestService.ashx");
            //    hfp.Params.Add("action", "uploadplugin");
            //    hfp.Params.Add("isCoverExist", "1");
            //    List<G.SL.Net.FileInfo> files = new List<SL.Net.FileInfo>();
            //    foreach (var file in open.Files)
            //    {
            //        using (var fileStream = file.Open(System.IO.FileMode.Open, System.IO.FileAccess.Read))
            //        {
            //            files.Add(new SL.Net.FileInfo() { ContentType = "file/dll", Name = System.IO.Path.GetFileNameWithoutExtension(fileStream.Name), FileName = System.IO.Path.GetFileName(fileStream.Name), FileBuffer = fileStream.GetBytes() });
            //        }
            //    }
            //    hfp.Files = files.ToArray();
            //    hfp.Request((ex, str) =>
            //    {
            //        var a = str;
            //    });
            //}
        }

        private void deleteTemplate_Click(object sender, RoutedEventArgs e)
        {
            FrameworkElement f = sender as FrameworkElement;
            G.APITest.Data.ProjectTemplate p = f.DataContext as G.APITest.Data.ProjectTemplate;
            Utils.MEFUtils.DeletePartFromISO(p);
        }

        private void getinternetPlugin_Expanded(object sender, Telerik.Windows.RadRoutedEventArgs e)
        {
            GloabalData.Instance.SampleClient.Execute(new G.Service.API.Request.GetGetPluginAdressRequest(), (c) =>
            {
                if (c.IsOK)
                {
                    internetPluginListBox.ItemsSource = c.ResponseData;
                }
            });
        }

        private void btnSetup_Click(object sender, RoutedEventArgs e)
        {
            FrameworkElement f = sender as FrameworkElement;
            string str = f.DataContext as string;
            Utils.MEFUtils.AddPartToIso(this, String.Format("{0}/{1}?t={2}", G.Service.API.APIConfig.BaseUrl, str, DateTime.Now.GetTimestamp()), System.IO.Path.GetFileNameWithoutExtension(str));
        }
    }
}

